'use strict'
let playerDao = require('../dao/playerDao')
  //会话管理类(用户连接管理类)
class PlayerSessionMgr {
  constructor() {
    this.sessions = {} //保存pid对应的player
  }

  bind(pId, player) {
    this.sessions[pId] = player
  }

  getPlayer(pId) {
    return this.sessions[pId]
  }

  delPlayer(pId) {
    delete this.sessions[pId]
  }

  savePlayer() {
    let sessions = this.sessions
    for (var i in sessions) {
      let player = sessions[i]
      if (player) {
        playerDao.savePlayerById(player._id, player, function(err, ret) {})
      }
    }
  }
}

let playerSessionMgr = new PlayerSessionMgr()
module.exports = playerSessionMgr

//每隔5分钟保存一次用户信息
setInterval(function() {
  playerSessionMgr.savePlayer()
}, 300000)
